package com.li.filter;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.hook.SendMessageContext;
import org.apache.rocketmq.client.hook.SendMessageHook;
import org.slf4j.MDC;

/**
 * @program: rocketmq_boot
 * @description:
 * @author: li
 * @create: 2023-03-08 10:35
 **/
public class RocketMqSendMessageHook implements SendMessageHook {
    @Override
    public String hookName() {
        return "RocketMqSendMessageHook";
    }

    @Override
    public void sendMessageBefore(SendMessageContext context) {
        context.getMessage().putUserProperty("traceId", "666666");
        // 获取当前线程的 mdc
        String traceId = MDC.get("traceId");
        if (StringUtils.isBlank(traceId)) {
            MDC.put("traceId", "MDC-666666");
        }
        System.out.println("traceId-666666");
    }

    @Override
    public void sendMessageAfter(SendMessageContext context) {

    }
}
